#!/bin/sh

# configure.

if [ ! -f mail.conf ]; then
    exit 0
else
    . mail.conf
fi


echo -n 'Enter user name:'
read name

if [ "$name" = "" ]; then
echo 'Must input name!'
exit
fi

echo -n 'Enter user domain:['${default_domain}']'
read domain
if [ "$domain" = "" ]; then
domain=${default_domain}
fi

echo "use ${mysql_db};" > delmailuser.sql
echo "select USER,NAME,DOMAIN,HOME,MAILDIR from postfix_users where (NAME='"$name"' and USER='"$name@$domain"' and DOMAIN='"$domain"');" >> delmailuser.sql
${mysql} -u$mysql_name -p$mysql_password < delmailuser.sql | grep $name@$domain > tmp_delmailuser

user=`awk '{ print $1 }' tmp_delmailuser`
name=`awk '{ print $2 }' tmp_delmailuser`
domain=`awk '{ print $3 }' tmp_delmailuser`
home=`awk '{ print $4 }' tmp_delmailuser`
maildir=`awk '{ print $5 }' tmp_delmailuser`

if [ "$home" = "" ]; then
    echo "No $name in $domain , please check and input again."
    rm -rf delmailuser.sql
    #rm -rf tmp_delmailuser
exit
fi

echo 'name= '$name
echo 'domain= '$domain
echo 'mail= '$name@$domain
echo 'home= '$home
echo 'maildir= '$maildir

echo -n "if under value is right,please input 'y' and Enter:"
read mychoice

if [ "$mychoice" = "y" ]; then
    echo "use ${mysql_db};" > delmailuser.sql
    echo "delete from postfix_users where (USER='"$name@$domain"' and NAME='"$name"' and DOMAIN='"$domain"');" >> delmailuser.sql
    ${mysql} -u$mysql_name -p$mysql_password < delmailuser.sql
    rm -rf delmailuser.sql
    rm -rf tmp_delmailuser
    MailUserDir=$home$domain/$name
    rm -rf $MailUserDir
    exit
fi

# chmod 755 /sbin/delmailuser
 

